//
//  AssetDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/12.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AssetDetailViewController.h"
#import "ResizeView.h"
#import "HttpCustomerAction.h"

@interface AssetDetailViewController (){
    AssetBlock assetBlock;
}
@property (nonatomic,strong) AssetInfo *assetInfo;

@end

@implementation AssetDetailViewController

-(id)initWithAssetInfo:(AssetInfo*)assetInfo block:(AssetBlock)block{
    if (self == [super init]) {
        self.assetInfo = assetInfo;
        assetBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"投资详情";
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
//    self.nameLabel.text = assetInfo.assetName;
//    self.amountLabel.text = [NSString stringWithFormat:@"金额:%.2f",assetInfo.amount];
//    self.yearLabel.text = [NSString stringWithFormat:@"年利率:%d%%",(int)(assetInfo.interestPerYear*100)];
//    self.timeLabel.text = [NSString stringWithFormat:@"时间:%@-%@", [Utils getTimeFront:assetInfo.startTime], [Utils getTimeFront:assetInfo.endTime]];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"资产名称" value:self.assetInfo.assetName]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"金额" value:[NSString stringWithFormat:@"%.2f",self.assetInfo.amount]]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"年利率" value:[NSString stringWithFormat:@"%d%%",(int)(self.assetInfo.interestPerYear*100)]]];
    
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"时间" value:[NSString stringWithFormat:@"%@-%@", [Utils getTimeFront:self.assetInfo.startTime], [Utils getTimeFront:self.assetInfo.endTime]]]];
    
    if ([self.assetInfo.status isEqualToString:@"due"]) {
        [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
    }

}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"赎回" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    [BlockAlertView showAlertWithTitle:@"确定赎回?" message:@"" alertViewStyle:UIAlertViewStyleDefault block:^(id clickView, NSInteger index) {
        if (index == 1) {
            [self showWaitingViewWithMessage:@"赎回中..." style:WaitingViewShowStyleFullSelfView];
            [HttpCustomerAction postRedeemAsset:self.assetInfo.id block:^(id result, NSError *error) {
                [self dismissWaitingView];
                if (!error) {
                    [self showMessage:@"赎回成功" autoDissmissAfterDelay:0];
                    if (assetBlock) {
                        assetBlock();
                    }
                    [self performBlock:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    } afterDelay:0.8];
                }
            }];
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *label = [UILabel new];
    label.text = valueString;
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentRight;
    label.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:label viewKey:@"TF"];
    label.sd_layout.topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10).widthIs(200);
    return resizeViewInfo;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
